package missPuff.com;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.com.VideoInfo;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LocalVideoAdapter extends BaseAdapter{
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private List<VideoInfo> list;
	private File file;
	private Context context;

	public LocalVideoAdapter(Context context, List<VideoInfo> list) {
		this.context = context;
		this.list = list;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = rowViews.get(position);
		if (rowView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.localvideolist_item, null);
			ImageView video_image = (ImageView) rowView
					.findViewById(R.id.localvideo_image);
			TextView video_title = (TextView) rowView
					.findViewById(R.id.localvideo_name);
			TextView video_size = (TextView) rowView
					.findViewById(R.id.localvideo_size);

			
			file = new File(list.get(position).getImage_localUrl());
			if(file.exists()){
				video_image.setImageBitmap(BitmapFactory.decodeFile(list.get(position).getImage_localUrl()));
			}
			else{
				video_image.setImageResource(R.drawable.video_pic);
			}
			float size = Float.parseFloat(String.valueOf(list.get(position).getVideo_size()));
			size = getSize(size);
			video_size.setText(size + "M");
			video_title.setText(list.get(position).getVideo_title());
		}

		return rowView;
	}	
	
	public float getSize(float   rSize){
		  int   scale   =   1;
		  int   roundingMode   =   4;
		  BigDecimal   bigDecimal   =   new   BigDecimal(rSize / 1024 / 1024);  
		  bigDecimal   =   bigDecimal.setScale(scale,roundingMode);  
		  rSize   =   bigDecimal.floatValue(); 
		  return rSize;
	}
}
